﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CuaHangBangDia;

namespace CuaHangBangDia.QLHoaDon
{
    public partial class HoaDonForm : Form
    {
        public static int maHD;
        double hs;
        private bool _selectAll = false;
        int countdgv;
        public HoaDonForm()
        {
            InitializeComponent();
            dgvHoaDon.AutoGenerateColumns = false;
        }

        int item = 15;
        int page = 1;
        int tong;
        private void LoadHoaDon(List<HoaDon> ds)
        {
            dgvHoaDon.DataSource = ds;
            List<HoaDon> list = HoaDon.Objects.GetAll().ToList();
            HienThiMaHoaDon();
            if (list.Count >= 1)
            {
                countdgv = 2;
            }
            tong = TongTrang(HoaDon.Objects.GetAll().ToList().Count, item);
            txtTrang.Text = Convert.ToString(page);
            lbTongTrang.Text = "/ " + tong;
        }

        public List<HoaDon> getPage(int page)
        {
            var query = from c in HoaDon.Objects.GetAll().ToList()
                           .Skip(item * page - item).Take(item)
                        select c;
            return query.ToList();

        }
        public List<HoaDon> getPageTK(int page)
        {
            DateTime t = dtpThoiGian.Value;
            var query = from c in HoaDon.Objects.Find(tkhd => (tkhd.NgayLap.Day == t.Day && tkhd.NgayLap.Month == t.Month && tkhd.NgayLap.Year == t.Year)).ToList()
                           .Skip(item * page - item).Take(item)
                        select c;
            return query.ToList();

        }
        public List<HoaDon> getPageTKT(int page)
        {
            DateTime t = dtpThoiGian.Value;
            var query = from c in HoaDon.Objects.Find(tkhd => (tkhd.NgayLap.Month == t.Month && tkhd.NgayLap.Year == t.Year)).ToList()
                           .Skip(item * page - item).Take(item)
                        select c;
            return query.ToList();

        }
        public List<HoaDon> getPageTKN(int page)
        {
            DateTime t = dtpThoiGian.Value;
            var query = from c in HoaDon.Objects.Find(tkhd => (tkhd.NgayLap.Year == t.Year)).ToList()
                           .Skip(item * page - item).Take(item)
                        select c;
            return query.ToList();

        }
        public static int TongTrang(int soluongdl, int num)
        {
            int kq = (int)(soluongdl / num);
            if (kq * num == soluongdl)
                return kq;
            return kq + 1;
        }


        private void btnHienThi_Click(object sender, EventArgs e)
        {

            gbPhanTrang.Visible = true;
            grbPhanTrangTimKiem.Visible = false;
            grbTKN.Visible = false;
            grbTKT.Visible = false;
            LoadHoaDon(getPage(1));
            txtTrang.Text = Convert.ToString(1);
            page = 1;

        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            ThemHoaDonForm thd = new ThemHoaDonForm();
            thd.ShowDialog();
        }

     

        private void btnXoa_Click(object sender, EventArgs e)
        {
            XoaHoaDon();
        }

        private void XoaHoaDon()
        {

            bool kq = false;
            List<HoaDon> list = LayHoaDon();
            if (list.Count > 0)
            {
                DialogResult que = MessageBox.Show("Bạn chắc chắn muốn Xóa " + list.Count + " hóa đơn đã chọn?", "Cảnh Báo",
                               MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (que == DialogResult.OK)
                {
                    foreach (HoaDon item in list)
                    {
                        kq = HoaDon.Objects.Delete(item);
                    }
                    if (kq)
                    {
                        MessageBox.Show("Xóa hóa đơn thành công !");
                        btnHienThi.PerformClick();
                    }
                }
                else
                {
                    for (int i = 0; i < dgvHoaDon.Rows.Count; i++)
                    {
                        dgvHoaDon.Rows[i].Cells[0].Value = false;
                    }
                }
            }
            else
                MessageBox.Show("Vui lòng chọn hóa đơn muốn xóa!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private List<HoaDon> LayHoaDon()
        {
            List<HoaDon> lists = new List<HoaDon>();
            if (dgvHoaDon.Rows.Count > 0)
            {
                for (int i = 0; i < dgvHoaDon.Rows.Count; i++)
                {
                    try
                    {
                        if (dgvHoaDon.Rows[i].Cells[0].Value != null && dgvHoaDon.Rows[i].Cells[0].Value.ToString() == "True")
                        {
                            HoaDon b = GetDataHoaDon(i);
                            lists.Add(b);
                            XoaCTHD(i);
                        }
                    }
                    catch (Exception)
                    {

                    }
                }
            }
            return lists;
        }
        private HoaDon GetDataHoaDon(int i)
        {
            HoaDon bd = new HoaDon();
            bd.MaHoaDon = Convert.ToInt32(dgvHoaDon.Rows[i].Cells[2].Value.ToString());
            bd.NgayLap = Convert.ToDateTime(dgvHoaDon.Rows[i].Cells[3].Value.ToString());
            bd.ThanhTien = Convert.ToDouble(dgvHoaDon.Rows[i].Cells[4].Value.ToString());
            bd.NhanVienLap = dgvHoaDon.Rows[i].Cells[5].Value.ToString();
            return bd;
        }
        private void XoaCTHD(int i)
        {
            ChiTietHoaDon c = new ChiTietHoaDon();
            int ma = Convert.ToInt32(dgvHoaDon.Rows[i].Cells[2].Value);
            List<ChiTietHoaDon> f = ChiTietHoaDon.Objects.Find(k => k.MaHoaDon == ma).ToList();
            foreach (ChiTietHoaDon item in f)
            {
                ChiTietHoaDon.Objects.Delete(item);

            }
        }

        private void LoadTKHoaDon(List<HoaDon> ds)
        {
            dgvHoaDon.DataSource = ds;
            DateTime t = dtpThoiGian.Value;
            List<HoaDon> list = HoaDon.Objects.Find(tkhd => (tkhd.NgayLap.Day == t.Day && tkhd.NgayLap.Month == t.Month && tkhd.NgayLap.Year == t.Year)).ToList();
            HienThiMaHoaDon();
            if (list.Count >= 1)
            {
                countdgv = 2;
            }
            tong = TongTrang(HoaDon.Objects.Find(tkhd => (tkhd.NgayLap.Day == t.Day && tkhd.NgayLap.Month == t.Month && tkhd.NgayLap.Year == t.Year)).ToList().Count, item);
            txtTrang.Text = Convert.ToString(page);
            lblTSTrang.Text = "/ " + tong;

        }
        private void LoadTKT(List<HoaDon> ds)
        {
            dgvHoaDon.DataSource = ds;
            DateTime t = dtpThoiGian.Value;
            List<HoaDon> list = HoaDon.Objects.Find(tkhd => (tkhd.NgayLap.Month == t.Month && tkhd.NgayLap.Year == t.Year)).ToList();
            HienThiMaHoaDon();
            if (list.Count >= 1)
            {
                countdgv = 2;
            }
            tong = TongTrang(HoaDon.Objects.Find(tkhd => (tkhd.NgayLap.Month == t.Month && tkhd.NgayLap.Year == t.Year)).ToList().Count, item);
            txtTrangHienTaiTKT.Text = Convert.ToString(page);
            lblTongSoTrangTKT.Text = "/ " + tong;

        }
        private void LoadTKN(List<HoaDon> ds)
        {
            dgvHoaDon.DataSource = ds;
            DateTime t = dtpThoiGian.Value;
            List<HoaDon> list = HoaDon.Objects.Find(tkhd => (tkhd.NgayLap.Year == t.Year)).ToList();
            HienThiMaHoaDon();
            if (list.Count >= 1)
            {
                countdgv = 2;
            }
            tong = TongTrang(HoaDon.Objects.Find(tkhd => (tkhd.NgayLap.Year == t.Year)).ToList().Count, item);
            txtTrangHienTaiTKN.Text = Convert.ToString(page);
            lblTongSoTrangTKN.Text = "/ " + tong;

        }

        private void btnTimKiemHoaDon_Click(object sender, EventArgs e)
        {
            if (rdNgay.Checked == false && rdThang.Checked == false && rdNam.Checked == false)
            {
                MessageBox.Show("Vui lòng chọn kiểu tìm kiếm!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                if (rdNgay.Checked)
                {
                    gbPhanTrang.Visible = false;
                    grbTKT.Visible = false;
                    grbTKN.Visible = false;
                    grbPhanTrangTimKiem.Visible = true;
                    LoadTKHoaDon(getPageTK(1));
                    txtTrangHienTai.Text = Convert.ToString(1);
                    page = 1;
                    
                }
                if (rdThang.Checked)
                {
                    gbPhanTrang.Visible = false;
                    grbTKT.Visible = true;
                    grbTKN.Visible = false;
                    grbPhanTrangTimKiem.Visible = false;
                    LoadTKT(getPageTKT(1));
                    txtTrangHienTaiTKT.Text = Convert.ToString(1);
                    page = 1;
                }
                if (rdNam.Checked)
                {
                    gbPhanTrang.Visible = false;
                    grbTKT.Visible = false;
                    grbTKN.Visible = true;
                    grbPhanTrangTimKiem.Visible = false;
                    LoadTKN(getPageTKN(1));
                    txtTrangHienTaiTKN.Text = Convert.ToString(1);
                    page = 1;
                }
            }
        }
        void TimKiemHoaDonTheoNgay(DateTime t)
        {


            dgvHoaDon.DataSource = HoaDon.Objects.Find(tkhd => (tkhd.NgayLap.Day == t.Day && tkhd.NgayLap.Month == t.Month && tkhd.NgayLap.Year == t.Year)).ToList();

        }
        void TimKiemHoaDonTheoThang(DateTime t)
        {


            dgvHoaDon.DataSource = HoaDon.Objects.Find(tkhd => (tkhd.NgayLap.Month == t.Month && tkhd.NgayLap.Year == t.Year)).ToList();

        }
        void TimKiemHoaDonTheoNam(DateTime t)
        {


            dgvHoaDon.DataSource = HoaDon.Objects.Find(tkhd => (tkhd.NgayLap.Year == t.Year)).ToList();

        }
        

        private void frmHoaDon_Load(object sender, EventArgs e)
        {
            countdgv = 1;
            gbPhanTrang.Visible = false;
            grbPhanTrangTimKiem.Visible = false;
            grbTKT.Visible = false;
            grbTKN.Visible = false;

        }
        void HienThiMaHoaDon()
        {
            string s = "HD";
            foreach (DataGridViewRow r in dgvHoaDon.Rows)
            {
                r.Cells[1].Value = s + r.Cells[2].Value.ToString();
            }
        }
        private void btnLui_Click(object sender, EventArgs e)
        {
            if (page >= 2)
            {
                page--;
                txtTrang.Text = Convert.ToString(page);
                LoadHoaDon(getPage(page));
            }
        }

        private void btnTien_Click(object sender, EventArgs e)
        {

            if (page < tong)
                page++;
            if (page <= tong)
            {
                txtTrang.Text = Convert.ToString(page);
                LoadHoaDon(getPage(page));
            }
        }

        private void txtTrang_KeyDown(object sender, KeyEventArgs e)
        {
            int t;
            if (e.KeyData == Keys.Enter)
            {
                try
                {
                    t = Convert.ToInt32(txtTrang.Text);
                    if (t <= tong && t > 0)
                    {
                        LoadHoaDon(getPage(t));
                        txtTrang.Text = Convert.ToString(t);
                        page = t;
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("Nhâp Trang Muốn Đến");
                }

            }
        }

        private void btnTDau_Click(object sender, EventArgs e)
        {
            if (page > 1)
            {
                LoadHoaDon(getPage(1));
                txtTrang.Text = Convert.ToString(1);
                page = 1;
            }
        }

        private void btnTCuoi_Click(object sender, EventArgs e)
        {
            if (page < tong)
            {

                LoadHoaDon(getPage(tong));
                txtTrang.Text = Convert.ToString(tong);
                page = tong;
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnVeDauTien_Click(object sender, EventArgs e)
        {
            if (page > 1)
            {
                LoadTKHoaDon(getPageTK(1));
                txtTrangHienTai.Text = Convert.ToString(1);
                page = 1;
            }
        }

        private void btnVeTruoc_Click(object sender, EventArgs e)
        {

            if (page >= 2)
            {
                page--;
                txtTrangHienTai.Text = Convert.ToString(page);
                LoadTKHoaDon(getPageTK(page));
            }
        }

        private void txtTrangHienTai_KeyDown(object sender, KeyEventArgs e)
        {
            int t;
            if (e.KeyData == Keys.Enter)
            {
                try
                {
                    t = Convert.ToInt32(txtTrang.Text);
                    if (t <= tong && t > 0)
                    {
                        LoadTKHoaDon(getPageTK(t));
                        txtTrang.Text = Convert.ToString(t);
                        page = t;
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("Nhâp Trang Muốn Đến");
                }

            }
        }

        private void btnTrangSau_Click(object sender, EventArgs e)
        {
            if (page < tong)
                page++;
            if (page <= tong)
            {
                txtTrangHienTai.Text = Convert.ToString(page);
                LoadTKHoaDon(getPageTK(page));
            }
        }

        private void btnCuoiCung_Click(object sender, EventArgs e)
        {
            if (page < tong)
            {

                LoadTKHoaDon(getPageTK(tong));
                txtTrangHienTai.Text = Convert.ToString(tong);
                page = tong;
            }
        }

        private void lậpHóaĐơnToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            ThemHoaDonForm thd = new ThemHoaDonForm();
            thd.ShowDialog();
        }

        private void btnCTHD_Click(object sender, EventArgs e)
        {
            if (dgvHoaDon.Rows.Count > 0 && countdgv==2)
            {
                ChiTietHoaDonForm ct = new ChiTietHoaDonForm();

                DataGridViewRow r = dgvHoaDon.CurrentRow;
                int mah = Convert.ToInt32(r.Cells[2].Value);
                ct.dgvChTietHoaDon.DataSource = ChiTietHoaDon.Objects.Find(k => k.MaHoaDon == mah).ToList();
                ct.lblThongBaoCTHD.Text = "HÓA ĐƠN: HD" + mah.ToString();
                List<HoaDon> kk = HoaDon.Objects.Find(rr => rr.MaHoaDon == mah).ToList();
                foreach (HoaDon item in kk)
                {
                    hs = item.ThanhTien;
                }
                ct.lblTongTienCTHD.Text = "Tổng tiền: " + hs.ToString() + " VND";
                ct.Show();
            }
            else
            {
                MessageBox.Show("Bạn chưa chọn hóa đơn cần xem!","Cảnh báo",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }
        }
        private void xóaHóaĐơnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            XoaHoaDon();
        }
        private void btnTruocTKN_Click(object sender, EventArgs e)
        {
            if (page >= 2)
            {
                page--;
                txtTrangHienTaiTKN.Text = Convert.ToString(page);
                LoadTKN(getPageTKN(page));
            }
        }

        private void btnTruocTKT_Click(object sender, EventArgs e)
        {
            if (page >= 2)
            {
                page--;
                txtTrangHienTaiTKT.Text = Convert.ToString(page);
                LoadTKT(getPageTKT(page));
            }
        }

        private void btnSauTKN_Click(object sender, EventArgs e)
        {
            if (page < tong)
                page++;
            if (page <= tong)
            {
                txtTrangHienTaiTKN.Text = Convert.ToString(page);
                LoadTKN(getPageTKN(page));
            }
        }

        private void btnSauTKT_Click(object sender, EventArgs e)
        {
            if (page < tong)
                page++;
            if (page <= tong)
            {
                txtTrang.Text = Convert.ToString(page);
                LoadTKT(getPageTKT(page));
            }
        }

        private void txtTrangHienTaiTKN_KeyDown(object sender, KeyEventArgs e)
        {
            int t;
            if (e.KeyData == Keys.Enter)
            {
                try
                {
                    t = Convert.ToInt32(txtTrang.Text);
                    if (t <= tong && t > 0)
                    {
                        LoadTKN(getPageTKN(t));
                        txtTrangHienTaiTKN.Text = Convert.ToString(t);
                        page = t;
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("Nhâp Trang Muốn Đến");
                }

            }
        }

        private void txtTrangHienTaiTKT_KeyDown(object sender, KeyEventArgs e)
        {
            int t;
            if (e.KeyData == Keys.Enter)
            {
                try
                {
                    t = Convert.ToInt32(txtTrang.Text);
                    if (t <= tong && t > 0)
                    {
                        LoadTKT(getPageTKT(t));
                        txtTrangHienTaiTKT.Text = Convert.ToString(t);
                        page = t;
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("Nhâp Trang Muốn Đến");
                }

            }
        }

        private void btnDauTKN_Click(object sender, EventArgs e)
        {
            if (page > 1)
            {
                LoadTKN(getPageTKN(1));
                txtTrangHienTaiTKN.Text = Convert.ToString(1);
                page = 1;
            }
        }

        private void btnDauTKT_Click(object sender, EventArgs e)
        {
            if (page > 1)
            {
                LoadTKT(getPageTKT(1));
                txtTrangHienTaiTKT.Text = Convert.ToString(1);
                page = 1;
            }
        }

        private void btnCuoiTKN_Click(object sender, EventArgs e)
        {
            if (page < tong)
            {

                LoadTKN(getPageTKN(tong));
                txtTrangHienTaiTKN.Text = Convert.ToString(tong);
                page = tong;
            }
        }

        private void btnCuoiTKT_Click(object sender, EventArgs e)
        {
            if (page < tong)
            {

                LoadTKT(getPageTKT(tong));
                txtTrangHienTaiTKT.Text = Convert.ToString(tong);
                page = tong;
            }
        }

        private void dgvHoaDon_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int n = dgvHoaDon.Rows.Count;
            if (e.ColumnIndex == 0 && n >= 0)
            {
                _selectAll = !_selectAll;
                for (int idx = 0; idx < n; idx++)
                {
                    dgvHoaDon.Rows[idx].Selected = _selectAll;
                    dgvHoaDon.Rows[idx].Cells[0].Value = _selectAll;
                }

            }
        }

        private void inToolStripMenuItem_Click(object sender, EventArgs e)
        {

            if (dgvHoaDon.Rows.Count > 0 && countdgv == 2)
            {
                DataGridViewRow r = dgvHoaDon.CurrentRow;
                int mah = Convert.ToInt32(r.Cells[2].Value);
                ThemHoaDon_ReportForm frm = new ThemHoaDon_ReportForm();
                frm.mahoadon = mah;
                frm.Show();
            }
            else
            {
                MessageBox.Show("Bạn chưa chọn hóa đơn cần in!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            if (dgvHoaDon.Rows.Count > 0 && countdgv==2)
            {
                DataGridViewRow r = dgvHoaDon.CurrentRow;
                int mah = Convert.ToInt32(r.Cells[2].Value);
                ThemHoaDon_ReportForm frm = new ThemHoaDon_ReportForm();
                frm.mahoadon = mah;
                frm.Show();
            }
            else
            {
                MessageBox.Show("Bạn chưa chọn hóa đơn cần in!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void chiTiếtHóaĐơnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgvHoaDon.Rows.Count > 0 && countdgv == 2)
            {
                ChiTietHoaDonForm ct = new ChiTietHoaDonForm();

                DataGridViewRow r = dgvHoaDon.CurrentRow;
                int mah = Convert.ToInt32(r.Cells[2].Value);
                ct.dgvChTietHoaDon.DataSource = ChiTietHoaDon.Objects.Find(k => k.MaHoaDon == mah).ToList();
                ct.lblThongBaoCTHD.Text = "HÓA ĐƠN: HD" + mah.ToString();
                ThemHoaDonForm j = new ThemHoaDonForm();
                List<HoaDon> kk = HoaDon.Objects.Find(rr => rr.MaHoaDon == mah).ToList();
                foreach (HoaDon item in kk)
                {
                    hs = item.ThanhTien;
                }
                ct.lblTongTienCTHD.Text = "Tổng tiền: " + hs.ToString() + " VND";
                ct.Show();
            }
            else
            {
                MessageBox.Show("Bạn chưa chọn hóa đơn cần xem!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void dgvHoaDon_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
